package com.common.baselibrary.utils;

import java.text.DecimalFormat;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    /**
     * 将字符串中的某个字符串替换为另外一个字符串
     *
     * @param template    操作的字符串
     * @param placeholder 被替换的字符串
     * @param replacement 替换的的字符串
     */
    public static String replace(String template, String placeholder, String replacement) {
        int loc = template.indexOf(placeholder);
        if (loc < 0) {
            return template;
        } else {
            return template.substring(0, loc) + replacement + replace(template.substring(loc + placeholder.length()), placeholder, replacement);
        }
    }

    /**
     * 将字符串分割，返回数组
     *
     * @param seperators 分割字符
     * @param list       字符串
     */
    public static String[] split(String seperators, String list) {
        StringTokenizer tokens = new StringTokenizer(list, seperators);
        String[] result = new String[tokens.countTokens()];
        int i = 0;
        while (tokens.hasMoreTokens()) {
            result[i++] = tokens.nextToken();
        }
        return result;
    }

    public static String root(String qualifiedName) {
        if (null != qualifiedName && qualifiedName.length() > 0) {
            int loc = qualifiedName.indexOf(".");
            return (loc < 0) ? qualifiedName : qualifiedName.substring(0, loc);
        }
        return qualifiedName;
    }

    public static String toString(Object[] array) {
        int len = array.length;
        if (len == 0)
            return "";
        StringBuilder buf = new StringBuilder(len * 12);
        for (int i = 0; i < len - 1; i++) {
            buf.append(array[i]).append(", ");
        }
        return buf.append(array[len - 1]).toString();
    }

    /**
     * 检查是否为有效的邮政编码
     *
     * @return
     */
    public static boolean checkZIP(String str) {
        Pattern p = Pattern.compile("^\\d{6}$");
        Matcher m = p.matcher(str);
        return m.matches();
    }

    public static String toString(Object obj) {
        return obj == null || obj.toString().trim().toUpperCase().equals("NULL") ? "" : obj.toString().trim();
    }

    public static boolean isEmpty(String str) {
        return ((str == null) || (str.length() == 0));
    }

    public static boolean isNotEmpty(String str) {
        return (!(isEmpty(str)));
    }

    public static boolean isBlank(String str) {
        int strLen;
        if ((str == null) || ((strLen = str.length()) == 0)) {
            return true;
        }
        for (int i = 0; i < strLen; ++i) {
            if (!(Character.isWhitespace(str.charAt(i)))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(String str) {
        return (!(isBlank(str)));
    }

    public static boolean equals(String str1, String str2) {
        return ((str1 != null) && ((str2 == null) || str1.equals(str2)));
    }

    /**
     * 金额转换为保留两位小数
     *
     * @param money 原金额
     * @return String 返回值
     */
    public static String getDecimalMoney(String money) {
        DecimalFormat df = new DecimalFormat("#0.00");
        double amount = 0;
        try {
            amount = Double.parseDouble(money);
        } catch (Exception e) {
            amount = 0;
        }
        return df.format(amount);
    }

    /**
     * 将List数组转换为sql的in格式
     *
     * @param list the list
     * @return string
     */
    public static String convertListToString(List<String> list) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String str : list) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(str);
        }
        return sb.toString();
    }
}
